package com.hdjk.view.activity.personalcenter;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.adapter.RiskTestListAdapter;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.config.Constant;
import com.hdjk.view.entity.RiskTestEntity;
import com.hdjk.view.entity.RiskTestReturnEntity;
import com.hdjk.view.network.Network;
import com.hdjk.view.util.ToastUtils;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * 风险评测页
 * Created by sxy on 2017/11/2.
 */

public class RiskTestListActivity extends BaseActivity implements View.OnClickListener {

    private List<RiskTestEntity> mList;
    private RiskTestListAdapter mAdapter;

    // Content View Elements
    private com.lst.projectlib.component.CustomView.TopTitleBar ttb_title_bar;
    private TextView tv_submit;
    private ListView lv_data_list;
    // End Of Content View Elements

    private void bindViews() {
        ttb_title_bar = (com.lst.projectlib.component.CustomView.TopTitleBar) findViewById(R.id.ttb_title_bar);
        tv_submit = (TextView) findViewById(R.id.tv_submit);
        lv_data_list = (ListView) findViewById(R.id.lv_data_list);
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_risk_test_list);
        bindViews();
        ttb_title_bar.findViewById(R.id.tvNavBack).setOnClickListener(this);
        tv_submit.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        mList = new ArrayList<>();
        mAdapter = new RiskTestListAdapter(this);
        mAdapter.setDataList(mList);

        lv_data_list.setAdapter(mAdapter);
        lv_data_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

        getNetworkList();
    }

    private void getNetworkList() {
        mDialog.show();
        Network.getRiskTestList(UserTokenUtil.getToken(RiskTestListActivity.this),new CallBackListener() {
            @Override
            public void onCallBackCompleted(Msg msg) {
                mDialog.dismiss();
                if (msg != null && msg.getSuccess() && msg.getData() != null) {
                    List<RiskTestEntity> tempList = (ArrayList) msg.getData();
                    if (null != tempList && tempList.size() > 0) {
                        Collections.sort(tempList);
                        mList = tempList;
                        if (mAdapter == null) {
                            mAdapter = new RiskTestListAdapter(RiskTestListActivity.this);
                            mAdapter.setDataList(mList);
                            lv_data_list.setAdapter(mAdapter);
                        } else {
                            mAdapter.setDataList(mList);
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }

            @Override
            public void onCallBackError(Msg msg) {
                mDialog.cancel();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvNavBack:
                back();
                break;
            case R.id.tv_submit:
                JSONObject answers=mAdapter.getAnswers();
                Iterator iterator=answers.keys();
                int index=0;
                while (iterator.hasNext()){
                    index++;
                    iterator.next();
                }
                if (index!=mList.size()){
                    ToastUtils.showShortToast(RiskTestListActivity.this,"请完成所有测评题目");
                    return;
                }
                JSONObject object=new JSONObject();
                try {
                    mDialog.show();
                    object.put("answers",answers.toString());
                    object.put("accountNumber", UserTokenUtil.getUserName(RiskTestListActivity.this));
                    Network.submitRiskTest(UserTokenUtil.getToken(RiskTestListActivity.this),object.toString(), new CallBackListener() {
                        @Override
                        public void onCallBackCompleted(Msg msg) {
                            mDialog.cancel();
                            if (msg!=null&&msg.getSuccess()&&msg.getData()!=null&&msg.getData() instanceof RiskTestReturnEntity){
                                Intent intent=new Intent(RiskTestListActivity.this,RiskTestReturnActivity.class);
                                Bundle bundle=new Bundle();
                                bundle.putSerializable("entity", (RiskTestReturnEntity) msg.getData());
                                intent.putExtras(bundle);
                                intent.putExtra("type",2);
                                startActivity(intent);

                                Intent intent1 = new Intent(Constant.INTENT_FILTER_STRING_AFTER_IFAST_RISK_TEST);
                                sendBroadcast(intent1);
                                back();
                            }else {
                                if (msg!=null&&!TextUtils.isEmpty(msg.getMsg())&&!"null".equals(msg.getMsg())){
                                    ToastUtils.showShortToast(RiskTestListActivity.this,msg.getMsg());
                                }else {
                                    ToastUtils.showShortToast(RiskTestListActivity.this,"提交失败");
                                }
                            }
                        }

                        @Override
                        public void onCallBackError(Msg msg) {
                            mDialog.cancel();
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
